<?php

/**
* 
*/
class CompaniesController extends AppController
{
	var $name = 'Companies';
	var $components = array('UserAuth', 'ImagesManagement');
	var $helpers = array('html');
	
	public function beforeFilter()
	{
		$this->set('project_id', 0);
		$this->set('project_name', "");
	}
	
	public function index()
	{
		if ($this->UserAuth->isAdmin() == false /*|| $this->UserAuth->isSmile() == false*/) {
			$this->Session->setFlash('Failure, you don\'t have the right to access this area.');
			$this->redirect ('/');
		}
		$user = $this->Session->read('User');
		if ($user) {
			$this->set('user', $user['User']);
		}
		$this->set('companies', $this->Company->findAll(null, null, array('Company.name')));
	}
	
	public function add()
	{
		if ($this->UserAuth->isAdmin() == false /*|| $this->UserAuth->isSmile() == false*/) {
			$this->Session->setFlash('Failure, you don\'t have the right to access this area.');
			$this->redirect ('/');
		}
		if (!empty($this->data)) {
			//pr($this->data['Company']);
			
			if ($this->Company->save($this->data['Company'])) {
				
				$idCompany = $this->Company->getLastInsertId();
				if(is_uploaded_file($this->data['Company']['file']['tmp_name'])) {
					$extensions = array('.png', '.gif', '.jpg', '.jpeg');
					$extension = strrchr($this->data['Company']['file']['name'], '.');
					if (in_array($extension, $extensions))
					{
					  $picture_temp = $this->data['Company']['file']['tmp_name'];
					  $picture = $this->data['Company']['file']['name'];
					  move_uploaded_file($picture_temp, WWW_ROOT.'/img/clients/'.$idCompany.$extension);
					  //$res = $this->ImagesManagement->resize_img(WWW_ROOT.'/img/clients/', WWW_ROOT.'/img/clients/', $idCompany.$extension, $idCompany, 200, 0);
					  
					  $this->Company->query("UPDATE companies SET url_logo = 'clients/".$idCompany.$extension."' WHERE id = '".$idCompany."'");
					} else {
						$this->Session->setFlash('The image : '.$this->data['Company']['file']['name'].' couldn\'t be loaded.');
					}
				}
						
				$this->Session->setFlash('The company has been added.');
				$this->redirect('/companies');
			}	
		}
	}
	
	public function view($company_id)
	{
		if ($this->UserAuth->isAdmin() == false /*|| $this->UserAuth->isSmile() == false*/) {
			$this->Session->setFlash('Failure, you don\'t have the right to access this area.');
			$this->redirect ('/');
		}
		$result = $this->Company->findById($company_id);
		if (empty($result)) {
			// The company doesn't exist.
			$this->Session->setFlash("The Company doesn't exist.");
			$this->redirect('/companies');
		}
		$this->set('company', $result['Company']);
	}
	
	public function edit($company_id)
	{
		if ($this->UserAuth->isAdmin() == false) {
			$this->Session->setFlash('Failure, you don\'t have the right to access this area.');
			$this->redirect ('/');
		}
		$result = $this->Company->findById($company_id);
		if (empty($result)) {
			// The company doesn't exist.
			$this->Session->setFlash("The Company doesn't exist.");
			$this->redirect('/companies');
		}
		$this->set('company', $result['Company']);
		if (!empty($this->data)) {
			if ($this->Company->save($this->data['Company'])) {
			
				$idCompany = $this->Company->getLastInsertId();
				if(is_uploaded_file($this->data['Company']['file']['tmp_name'])) {
					$extensions = array('.png', '.gif', '.jpg', '.jpeg');
					$extension = strrchr($this->data['Company']['file']['name'], '.');
					if (in_array($extension, $extensions))
					{
					  $picture_temp = $this->data['Company']['file']['tmp_name'];
					  $picture = $this->data['Company']['file']['name'];
					  move_uploaded_file($picture_temp, WWW_ROOT.'/img/clients/'.$idCompany.$extension);
					  //$res = $this->ImagesManagement->resize_img(WWW_ROOT.'/img/clients/', WWW_ROOT.'/img/clients/', $idCompany.$extension, $idCompany, 200, 0);
					  
					  $this->Company->query("UPDATE companies SET url_logo = 'clients/".$idCompany.$extension."' WHERE id = '".$idCompany."'");
					} else {
						$this->Session->setFlash('The image : '.$this->data['Company']['file']['name'].' couldn\'t be loaded.');
					}
				}
				
				$this->Session->setFlash('The company: '.$result['Company']['name'].' has been edited.');
				$this->redirect('/companies');
			}		
		}		
	}
	
	public function del($company_id)
	{
		if ($this->UserAuth->isAdmin() == false) {
			$this->Session->setFlash('Failure, you don\'t have the right to access this area.');
			$this->redirect ('/');
		}
		$result = $this->Company->findById($company_id);
		if (empty($result)) {
			// The company doesn't exist.
			$this->Session->setFlash("The Company doesn't exist.");
			$this->redirect('/companies');
		}
		$this->Company->query("DELETE FROM companies WHERE id = '".$company_id."'");
		$this->Session->setFlash("The Company : ".$result['Company']['name']." has been deleted.");
		$this->redirect('/companies');		
	}
}

?>